﻿using FjordagerGymnastikHvorBorMedlemmerne;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.IO;

namespace TestProject
{
    
    
    /// <summary>
    ///This is a test class for ConvertAdressToCoordinateTest and is intended
    ///to contain all ConvertAdressToCoordinateTest Unit Tests
    ///</summary>
    [TestClass()]
    public class ConvertAdressToCoordinateTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        string INPUTFILEPATH = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) +  @"\datafil.txt";
        string OUTPUTFILEPATH = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) +  @"\output.kml";
        

        /// <summary>
        ///A test for Convert2LatLong
        ///</summary>
        [TestMethod()]
        public void Convert2LatLongTest()
        {
            ConvertAdressToCoordinate target = new ConvertAdressToCoordinate(); // TODO: Initialize to an appropriate value
            List<Coordinate> coordinatelist = new List<Coordinate>();
            Coordinate c = new Coordinate();
            c.X = 6144604.49;
            c.Y = 591677.21;
            coordinatelist.Add(c);
            Coordinate  actual = target.Convert2LatLong(c);
            Assert.AreEqual(55.4390450821049, actual.Y);
            Assert.AreEqual(10.4490813762058, actual.X);
            
        }

        /// <summary>
        ///A test for GenerateKML
        ///</summary>
        [TestMethod()]
        public void GenerateKMLTest()
        {
            ConvertAdressToCoordinate target = new ConvertAdressToCoordinate(); 
            Coordinate c = new Coordinate();
            c.X = 6144604.49;
            c.Y = 591677.21;
            Coordinate Position = target.Convert2LatLong(c);
            List<Coordinate> positions = new List<Coordinate>();
            positions.Add(Position);
            target.GenerateKML(positions);
            target.SaveKML(OUTPUTFILEPATH);
            
        }
        [TestMethod]
        public void EndToEndTest()
        {
            ConvertAdressToCoordinate target = new ConvertAdressToCoordinate();
            List<string> errors;
            string kml = target.GenerateKML(null, INPUTFILEPATH, out errors);
            target.SaveKML(OUTPUTFILEPATH);
        }
        
        /// <summary>
        ///A test for ParseAdresses
        ///</summary>
        [TestMethod()]
        public void ParseAdressesTest()
        {
            ConvertAdressToCoordinate_Accessor target = new ConvertAdressToCoordinate_Accessor(); // TODO: Initialize to an appropriate value
            string[] adresses = System.IO.File.ReadAllLines(INPUTFILEPATH, System.Text.Encoding.GetEncoding("iso-8859-1")); // TODO: Initialize to an appropriate value
            List<Coordinate> actual;
            List<string> errors = new List<string>();
            actual = target.ParseAdresses(null,adresses, out errors);
            Assert.AreEqual(13, actual.Count);

            // two lines should fail
            Assert.AreEqual(4, errors.Count);
            double n0 = 10.4490813762058;
            double e0 = 55.4390450821049;
            Assert.AreEqual(n0, actual[0].X);
            Assert.AreEqual(e0, actual[0].Y);
            Assert.AreEqual("test", actual[0].Description);
            double n2 = 10.4039027775603;
            double e2 = 55.394131326659;
            Assert.AreEqual(n2, actual[2].X);
            Assert.AreEqual(e2, actual[2].Y);
            Assert.AreEqual("et eller andet", actual[2].Description);

        }

        [TestMethod]
        public void testadresseswithSpaces()
        {
            ConvertAdressToCoordinate_Accessor target = new ConvertAdressToCoordinate_Accessor(); // TODO: Initialize to an appropriate value
            string[] adresses = new string[5];
            adresses[4] = "Piletoften 34 A;5320;Else Mortensen ";
            adresses[1] = "Nyborgvej 23 stuen tv.;5000;et eller andet";
            adresses[2] = "Nyborgvej 23 A stuen tv.;5000;et eller andet";
            adresses[3] = "Bjerggårds Alle 105a;5240;Tanja Hansen";
            adresses[0] = "Lunden 6a;5320;3: Betina Lykke Rasmussen (V)";
            List<string> errors = new List<string>();
            List<Coordinate> coors =  target.ParseAdresses(null, adresses, out errors);
            Assert.AreEqual(0, errors.Count);
            string result = target.GenerateKML(null, adresses, out errors);
            Assert.AreEqual(0, errors.Count);

        }


        //[TestMethod]
        //public void testKMLfile()
        //{
        //    ConvertAdressToCoordinate_Accessor target = new ConvertAdressToCoordinate_Accessor(); // TODO: Initialize to an appropriate value
        //    string[] adresses = System.IO.File.ReadAllLines(INPUTFILEPATH, System.Text.Encoding.GetEncoding("iso-8859-1")); // TODO: Initialize to an appropriate value
        //    List<string> errors;
        //    string res = target.GenerateKML(null, adresses, out errors);
        //    string refKMLContent = Properties.Settings.Default["refKMLContent"].ToString();
        //    Assert.AreEqual(res, refKMLContent);
            
        //}


    }
}
